// pages/add/add.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    defaultAddress: true,
    sushe: '',
    number: '',
    name: '',
    Phonenumber: '',

  },
  /* 获取宿舍名称 */
  getsushe(res) {
    //console.log(res)
    this.setData({
      sushe: res.detail.value
    })
  },
  /* 获取详细地址 */
  getnumber(res) {
    //console.log(res)
    this.setData({
      number: res.detail.value
    })
  },
  /* 获取收件姓名 */
  getname(res) {
    //console.log(res)
    this.setData({
      name: res.detail.value
    })
  },
  /* 获取收件电话 */
  getPhonenumber(res) {
    /* console.log(res) */
    this.setData({
      Phonenumber: res.detail.value
    })
  },
  /* 默认按钮设置 */
  default (res) {
    this.setData({
      defaultAddress: res.detail.value
    })
  },
  save() {
    //const that = this.data;
    //console.log(that.sushe,that.number,that.name,that.Phonenumber,that.defaultAddress)
    const {
      sushe,
      number,
      name,
      Phonenumber,
      defaultAddress
    } = this.data;
    let address = wx.getStorageSync('address');
    if (defaultAddress) {
      if (address) {
        for (let i = 0; i < address.length; i++) {
          if (address[i].defaultAddress) {
            wx.showToast({
              icon: 'error',
              title: '已存在默认地址',
            })
            return;
          }
        }
      }
    };
    const form = {
      sushe,
      number,
      name,
      Phonenumber,
      defaultAddress
    };
    if (!address) {
      address = [form];
    } else {
      address.push(form);
    }
    wx.setStorageSync('address', address);
    wx.navigateTo({
      url: '/pages/selectAddress/selectAddress',
    })
  },

  /*   defaultAddress: true,
    sushe: '',
    number: '',
    name: '',
    Phonenumber: '', */



  /**
   * 生命周期函数--监听页面加载,取出值
   */
  onLoad: function (options) {
    const { sushe,address } = options;
    const { sushe: sushes,number, name , Phonenumber, defaultAddress} = JSON.parse(address);
    this.setData({sushe,sushe: sushes,number,name,Phonenumber,efaultAddress,})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})